package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"quest/models"
	"strconv"
	"time"
)

type QuestController struct {
	beego.Controller
}
type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *QuestController) Ask() {

	title := c.GetString("title")
	content := c.GetString("content")
	img := c.GetString("img")
	CateId, _ := c.GetInt("CateId")
	question := models.Quest{

		Title:   title,
		Content: content,
		Img:     img,
		CateId:  uint(CateId),
	}
	err := models.DB.Create(&question).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: question,
		}
		c.ServeJSON()
	}
}

func (c *QuestController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "请选择上传的图片",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonR{
			Code: 1,
			Msg:  "文件上传过大，上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	path := "static/img" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		imgSrc := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "上传成功",
			Data: imgSrc,
		}
		c.ServeJSON()
	}

}

func (c *QuestController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 4)
	pageData := make(map[string]interface{})
	key := "page" + strconv.Itoa(page)
	val, err := models.RedisDB.Get(key).Bytes()
	if err != nil {
		var quest []models.Quest
		query := models.DB.Model(&models.Quest{})
		var count int64
		query.Count(&count)
		offset := (page - 1) * size

		err = query.Preload("QuesType").Offset(offset).Limit(size).Find(&quest).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			pageData["count"] = count
			pageData["quest"] = quest
			jsonRbyte, _ := json.Marshal(&pageData)
			value := string(jsonRbyte)
			models.RedisDB.Set(key, value, time.Second*20)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "查询成功-mysql",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		//models.RedisDB.Set(key, value, time.Second*20)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}
}
func (c *QuestController) Info() {
	id, _ := c.GetInt("id")
	key := "id" + strconv.Itoa(id)
	val, err := models.RedisDB.Get(key).Bytes()
	var quest models.Quest
	if err != nil {

		err = models.DB.Preload("QuesType").Where("id=?", id).First(&quest).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {

			jsonRbyte, _ := json.Marshal(&quest)
			value := string(jsonRbyte)
			models.RedisDB.Set(key, value, time.Second*20)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "详情展示成功-mysql",
				Data: quest,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &quest)
		//models.RedisDB.Set(key, value, time.Second*20)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: quest,
		}
		c.ServeJSON()
	}
}
